package com.yzycoc.coc.gf.function;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yzycoc.coc.gf.config.CocConfig;
import com.yzycoc.coc.gf.constant.ApiUrlConstant;
import com.yzycoc.coc.gf.info.CocBinding;
import com.yzycoc.coc.gf.info.Result;
import com.yzycoc.coc.gf.service.CocService;
import com.yzycoc.coc.gf.util.CocUtil;
import com.yzycoc.code.special.ApiSpecialCode;
import com.yzycoc.processor.event.EventPublicMessage;
import com.yzycoc.processor.send.info.ClientInfos;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 绑定相关功能相关功能
 * 创建时间： 2023/9/7
 *
 * @author yzy
 * @version 1.0
 **/
@Slf4j
public class BinDingPublicFunction {

    public static Result<JSONObject> get(String qqCode, String msg, String type, String clientType) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("qqcode", qqCode);
        paramMap.put("msg", msg);
        paramMap.put("clientType", clientType);
        paramMap.put("type", type);
        Result<JSONObject> cocBinding = CocService.httpPost(ApiUrlConstant.BINDING_GET, paramMap);
        return cocBinding;
    }


    /**
     * 删除绑定
     */
    public static boolean remove(EventPublicMessage eventPublicMessage, String msg, String[] split) {
        if (split.length < 3) {
            return false;
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("qqcode", eventPublicMessage.getSendUser());
        paramMap.put("msg", split[2]);
        Result<Object> objectResult = CocService.httpPost(ApiUrlConstant.BINDING_REMOVE, paramMap);
        CocConfig.API.sendPublicMsg(objectResult.getMessage(), eventPublicMessage);
        return true;
    }

    /**
     * 新增绑定
     */
    public static boolean add(EventPublicMessage eventPublicMessage, String msg, String[] split, String type) {
        if (split.length < 3) {
            return false;
        }
        String msgCode = split[3];
        if (CocUtil.containsSpecialChars(msgCode)) {
            CocConfig.API.sendPublicMsg(ApiSpecialCode.AT(eventPublicMessage.getSendUser()) + " 触发词不能包含特殊符号！", eventPublicMessage);
            return true;
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("qqcode", eventPublicMessage.getSendUser());
        paramMap.put("tag", split[2]);
        paramMap.put("msg", msgCode);
        paramMap.put("type", type);
        ClientInfos clientInfos = CocConfig.API.clientInfos(eventPublicMessage.getClient());
        if (ObjectUtil.isEmpty(clientInfos)) {
            CocConfig.API.sendPublicMsg(ApiSpecialCode.AT(eventPublicMessage.getSendUser()) + "当前存在非法未注册的组件，无法进行接下来的操作！", eventPublicMessage);
            return true;
        }
        paramMap.put("clientType", clientInfos.getClientType());
        Result<Object> result = CocService.httpPost(ApiUrlConstant.BINDING_ADD, paramMap);
        CocConfig.API.sendPublicMsg(result.getMessage(), eventPublicMessage);
        return true;
    }

    public static boolean select(EventPublicMessage eventPublicMessage, String msg, String[] split) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("qqCode", eventPublicMessage.getSendUser());
        ClientInfos clientInfos = CocConfig.API.clientInfos(eventPublicMessage.getClient());
        if (ObjectUtil.isEmpty(clientInfos)) {
            CocConfig.API.sendPublicMsg(ApiSpecialCode.AT(eventPublicMessage.getSendUser()) + "当前存在非法未注册的组件，无法进行接下来的操作！", eventPublicMessage);
            return true;
        }
        paramMap.put("clientType", clientInfos.getClientType());
        Result<JSONArray> result = CocService.httpPost(ApiUrlConstant.BINDING_LIST, paramMap);
        if (!result.isSuccess()) {
            CocConfig.API.sendPublicMsg(ApiSpecialCode.AT(eventPublicMessage.getSendUser()) + " 服务器通讯异常，请稍后尝试！", eventPublicMessage);
            return true;
        }
        List<CocBinding> dataList = result.getResult().toJavaList(CocBinding.class);
        if (CollUtil.isEmpty(dataList)) {
            CocConfig.API.sendPublicMsg(ApiSpecialCode.AT(eventPublicMessage.getSendUser()) + " 您还没有绑定过您的部落及村庄呢！", eventPublicMessage);
            return true;
        }
        StringBuffer sendMsg = new StringBuffer();
        sendMsg.append("≮-用户：" + ApiSpecialCode.AT(eventPublicMessage.getSendUser()) + "-≯\r\n" + "◎━━-绑定列表-━━◎\n ※类型  丨  ※标签  丨  ※触发词");
        for (CocBinding data : dataList) {
            sendMsg.append("\n" + data.getType() + "|#" + data.getTag() + "|" + data.getMsg());
        }
        sendMsg.append("\n◎━━━----◎----━━━◎");
        CocConfig.API.sendPublicMsg(sendMsg.toString(), eventPublicMessage);
        return true;
    }
}
